package com.junsi.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.junsi.bean.Client;
import com.junsi.bean.Department;
import com.junsi.bean.Record;
import com.junsi.bean.Sale;
import com.junsi.bean.SaleTrack;
import com.junsi.bean.Salestage;
import com.junsi.bean.Sources;
import com.junsi.bean.Staff;
import com.junsi.service.ClientService;
import com.junsi.service.DepartmentSerivce;
import com.junsi.service.RankService;
import com.junsi.service.RecordService;
import com.junsi.service.SaleService;
import com.junsi.service.SaleTrackService;
import com.junsi.service.SalestageService;
import com.junsi.service.SourcesService;
import com.junsi.service.StaffSerivce;
import com.junsi.util.InteractiveUtil;
import com.junsi.util.PageUtil;












import com.junsi.util.SaleTrackUtil;
import com.junsi.util.SaleUtil;


/**
 * 销售机会控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/sale")
public class SaleController {
	
	@Autowired
	private RecordService recordService ;
	
	@Autowired
	private RankService rankService ;
	
	@Autowired
	private SaleTrackService saleTrackService ;
	
    @Autowired
    private SaleService saleService;
   
    @Autowired
    private StaffSerivce staffSerivce;
   
    @Autowired
    private DepartmentSerivce departmentSerivce;
   
    @Autowired
    private ClientService clientService;
   
    @Autowired
    private SalestageService salestageService;
   
    @Autowired
    private SourcesService sourcesService;
   
    Logger logger=Logger.getRootLogger();
	/**
	 * 分页查询所有销售机会信息
	 * 
	 * @param 
	 */
	@RequestMapping("/findSale")
	public ModelAndView pageFindSale(PageUtil page,HttpServletRequest request){
		//获取员工ID
		String idString = (String) request.getSession().getAttribute("staffs");
		ModelAndView modelAndView = new ModelAndView();
		page.setPageSize(100);
		page.setPageNum(1);
		if(idString!=null){
		Integer count = saleService.findCount();
		if(count != null){
			page.setRowCount(count);
			//根据员工ID来查找销售机会
			String[] id = idString.split(","); 
			List<Sale> sales = saleService.pageFind(id, page.getEndIndex(), page.getBeginIndex());
			List<SaleUtil> saleUtils = new ArrayList<>();
			if(sales!=null){
		for (Sale sale : sales) {
			SaleUtil saleUtil = new SaleUtil();
			saleUtil.setSale(sale);
			saleUtils.add(saleUtil);
		}
		}
		int i = 0;
		for (SaleUtil saleUtil : saleUtils) {
			if(saleUtil.getSale().getSourceId()!=null){
				Sources sources=sourcesService.findById(saleUtil.getSale().getSourceId()); 
				if(sources!=null){
					saleUtils.get(i).setSources(sources.getSourceName());
				}
			}
			if(saleUtil.getSale().getSalestageId()!=null){
				Salestage salestage=salestageService.findById(saleUtil.getSale().getSalestageId());
				if(salestage!=null){
					saleUtils.get(i).setSalestage(salestage.getSaleStageName());
				}
			}
			if(saleUtil.getSale().getDepartmentId()!=null){
				Department department=departmentSerivce.findById(saleUtil.getSale().getDepartmentId());
				if(department!=null){
					saleUtils.get(i).setDepartment(department.getDepartment_name());
				}
			}
			if(saleUtil.getSale().getStaffId()!=null){
				Staff staff = staffSerivce.findById(saleUtil.getSale().getStaffId());
				if(staff!=null){
					saleUtils.get(i).setStaff(staff.getStaff_name());
				}
			}
			if(saleUtil.getSale().getClientId()!=null){
				Client client = clientService.findById(saleUtil.getSale().getClientId());
				if(client!=null){
					saleUtils.get(i).setClient(client.getClient_name());
				}
			}
			i++;
		}
		//显示新增时下拉框里的名字选项
		List<Client> client=clientService.findByAlls(id);
 		List<Department> departments=departmentSerivce.findByAll();
 		List<Sources> sources=sourcesService.listAll();
 		List<Salestage> salestages=salestageService.listAll();
 		request.getSession().setAttribute("client", client);
 		request.getSession().setAttribute("departments", departments);
 		request.getSession().setAttribute("sources", sources);
 		request.getSession().setAttribute("salestages", salestages);
 		
 		Double integer = 0.0;
 		for (Sale sale : sales) {
 			if(Double.valueOf(sale.getSaleMoney()) != null){
 				integer += Double.valueOf(sale.getSaleMoney());
 			}
			
		}
 		
		//统计金额
		modelAndView.addObject("money", integer);
		modelAndView.addObject("list", saleUtils);
		modelAndView.setViewName("forward:/salejihui/html/sale.jsp");	
		}
	}
		return modelAndView;
	}
	
	/**
	 * 销售机会新增
	 */
 	@RequestMapping("/insertSale")
 	public ModelAndView insertSale(HttpServletResponse response, Sale sale,HttpServletRequest request){
 		ModelAndView modelAndView=new ModelAndView();
 		if(sale!=null){
 				sale.setCreateDate(new Date());
 				Boolean result=saleService.save(sale);
 		}
 		modelAndView.setViewName("redirect:/sale/findSale.action");
		return modelAndView;	
 	}
 	
 	
 	/**
	 * 销售机会编辑
	 */
 	@RequestMapping("/updateSale")
 	public ModelAndView updateSale(HttpServletResponse response, Sale sale,HttpServletRequest request){
 		ModelAndView modelAndView=new ModelAndView();
 		saleService.update(sale);
 		modelAndView.setViewName("redirect:/sale/findSaleId.action?id="+sale.getSaleId());
		return modelAndView;
 	}
 	
 	/**
	 * 销售机会重启
	 */
 	@RequestMapping("/updateSale1")
 	@ResponseBody
 	public Integer updateSale1(HttpServletResponse response, Sale sale){
 		saleService.update(sale);
 		return sale.getSaleId();
 	}
 	

	/**
	 * 批量删除
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteManyId")
	public  void deleteManyId(String delid,HttpServletResponse response){
		boolean reuslt = false;
		if(delid != null){
			String[] delid1=delid.split(",");
			for (String string : delid1) {
				Integer del=Integer.parseInt(string);
				reuslt= saleService.deleteById(del);
			}
			if(reuslt){
				InteractiveUtil.WriteStringForHtml("删除成功", response);
			}
			else{
				InteractiveUtil.WriteStringForHtml("删除失败", response);
			}
				
			}
		InteractiveUtil.WriteStringForHtml("删除失败", response);
	}
	/**
	 * 销售机会ID查询对象
	 */
	@RequestMapping("/findSaleId")
	public ModelAndView findSaleId(Integer id,PageUtil page, Model model) {
		ModelAndView modelAndView = new ModelAndView();
		page.setPageSize(100);
		page.setPageNum(1);
		if(id!=null){
		Sale sale = saleService.findByID(id);
		if(sale!=null){
			SaleUtil saleUtil = new SaleUtil();
			saleUtil.setSale(sale);
			if(saleUtil.getSale().getStaffId()!=null){
				Staff staff = staffSerivce.findById(saleUtil.getSale().getStaffId());
				if(staff!=null){
					saleUtil.setStaff(staff.getStaff_name());
				}
			}
			if(saleUtil.getSale().getClientId()!=null){
				Client client = clientService.findById(saleUtil.getSale().getClientId());
				if(client!=null){
					saleUtil.setClient(client.getClient_name());
				}
			}
			if(saleUtil.getSale().getSourceId()!=null){
				Sources sources=sourcesService.findById(saleUtil.getSale().getSourceId());
				if(sources!=null){
					saleUtil.setSources(sources.getSourceName());
				}
			}
			if(saleUtil.getSale().getSalestageId()!=null){
				Salestage salestage=salestageService.findById(saleUtil.getSale().getSalestageId()); 
				if(salestage!=null){
					saleUtil.setSalestage(salestage.getSaleStageName());
				}
			}
			if(saleUtil.getSale().getDepartmentId()!=null){
				Department department=departmentSerivce.findById(saleUtil.getSale().getDepartmentId());
				if(department!=null){
					saleUtil.setDepartment(department.getDepartment_name());
				}
			}

			//显示所有记录类型
			List<Record> records=recordService.listAll();
			//根据saleid查找对应的线索,显示到销售机会详情哪里
			Integer count = saleTrackService.findCount();
			if(count != null){
				page.setRowCount(count);
				List<SaleTrack> saleTrack1 = saleTrackService.findBysaleId(id, page.getEndIndex(), page.getBeginIndex());
			if(saleTrack1!=null){
				List<SaleTrackUtil> saleTrackUtils = new ArrayList<>();
				for (SaleTrack saleTrack : saleTrack1) {
					SaleTrackUtil saleTrackUtilsss = new SaleTrackUtil();
					saleTrackUtilsss.setSaleTrack(saleTrack);
					saleTrackUtils.add(saleTrackUtilsss);
				}
		
		int i = 0;
		for (SaleTrackUtil saleTrackUtil2 : saleTrackUtils) {
			if(saleTrackUtil2.getSaleTrack().getRecordId()!=null){
				Record record=recordService.findById(saleTrackUtil2.getSaleTrack().getRecordId());
				if(record!=null){
					saleTrackUtils.get(i).setRecord(record.getRecordName());
				}
			}
			if(saleTrackUtil2.getSaleTrack().getStaffId()!=null){
				Staff staff1=staffSerivce.findById(saleTrackUtil2.getSaleTrack().getStaffId());
				if(staff1!=null){
					saleTrackUtils.get(i).setStaff(staff1.getStaff_name());
				}
			}
			if(saleTrackUtil2.getSaleTrack().getSaleId()!=null){
				Sale sale1 = saleService.findByID(saleTrackUtil2.getSaleTrack().getSaleId());
				if(sale1!=null){
					saleTrackUtils.get(i).setSale(sale.getSaleOpportunityName());
				}
			}
			i++;
		}	
		
		//显示销售阶段
		List<Salestage> salestages=salestageService.listAll();
		
		modelAndView.addObject("salestages", salestages);
		modelAndView.addObject("saletrack", saleTrackUtils);
		modelAndView.addObject("list", saleUtil);
		modelAndView.addObject("records", records);
		modelAndView.setViewName("forward:/salejihui/html/salexiangqing.jsp");
			}
			}
		}
		}	
		return modelAndView;	 
	}
	
	
	
	
}
